package com.yate.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yate.auth.service.ISysRoleService;
import com.yate.common.result.Result;
import com.yate.model.system.SysRole;
import com.yate.vo.system.AssginRoleVo;
import com.yate.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ Tool：IntelliJ IDEA
 * @ Author：云生
 * @ Date：2024-11-29-1:07
 * @ Version：1.0
 * @ Description：
 */
@Api(tags = "角色管理")
@RestController
@Slf4j
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     *@Author: yate
     *@Description:条件分页查询
     *@param: page 当前页码，limit 每页显示条数，roleName 角色名称，status 角色状态
     *@return: Result
     */
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo) {
        //调用service的方法实现
        //1 创建Page对象，传递分页相关参数
        //page 当前页  limit 每页显示记录数
        Page<SysRole> pageParam = new Page<>(page,limit);

        //2 封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)) {
            //封装 like模糊查询
            wrapper.like(SysRole::getRoleName,roleName);
        }

        //3 调用方法实现
        IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.success(pageModel);
    }

    /**
     *@Author: yate
     *@Description:添加角色
     *@param:
     *@return: Result
     */
    @ApiOperation("添加角色")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    public Result save(@RequestBody SysRole sysRole){
        //调用service方法
        boolean flag = sysRoleService.save(sysRole);
        if (flag) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //根据id查询角色信息
    @ApiOperation("根据id查询角色信息")
    @GetMapping ("get/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result getById(@PathVariable Long id){
        log.info("修改角色id：{}，角色信息：{}", id);
        //调用service方法
        SysRole sysRole1 = sysRoleService.getById(id);
        log.info("修改角色结果：{}", sysRole1);
        return Result.success(sysRole1);
    }

    /**
     *@Author: yate
     *@Description:修改角色
     *@param: sysRole
     *@return:  Result
     */
    @ApiOperation("修改角色")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    public Result update(@RequestBody SysRole sysRole) {

        boolean flag = sysRoleService.updateById(sysRole);
        if (flag) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     *@Author: yate
     *@Description:根据id删除角色
     *@param:
     *@return:
     */

    @ApiOperation("根据id删除角色")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result removeById(@PathVariable Long id) {
        boolean flag = sysRoleService.removeById(id);
        if (flag) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id批量删除角色")
    @DeleteMapping ("removeBatch")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result removeByIds(@RequestBody List<Long> idList) {
        boolean flag = sysRoleService.removeByIds(idList);
        if (flag) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     *@Author: yate
     *@Description: 查询所有角色 和 当前用户的角色
     *@param:
     *@return:
     */

    @ApiOperation(value = "根据用户获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> map = sysRoleService.findRoleDataByUserId(userId);
        return Result.success(map);
    }

    /**
     *@Author: yate
     *@Description: 根据用户分配角色
     *@param:
     *@return:
     */

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.success();
    }
}
